package com.wink_172.activity

import android.content.Context
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.widget.TextView
import androidx.databinding.DataBindingUtil
import com.wink_172.commons.R
import com.wink_172.commons.databinding.ActivityFirstBinding
import com.wink_172.commons.databinding.ActivityTestWebBinding
import com.wink_172.library.activity.SmartActivity
import com.wink_172.library.callback.ICallback
import com.wink_172.library.model.Constants
import com.wink_172.library.view.ActionBarView
import com.wink_172.utils.HttpRequest
import org.xutils.common.Callback
import org.xutils.x

/**
 * 网络连接测试
 * **/
class TestWebActivity : SmartActivity() {
    lateinit var binding: ActivityTestWebBinding

    companion object {
        open fun startActivity(context: Context) {

            //apply用法 kotlin-P132
            val intent = Intent(context, TestWebActivity::class.java).apply {
//                putExtra(Constants.PARAM_DATA1, "hello01");
//                putExtra(Constants.PARAM_DATA2, "hello02");
//                putExtra(Constants.PARAM_DATA3, "hello03");
            }

            context.startActivity(intent)
        }

    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        binding = DataBindingUtil.setContentView(this, R.layout.activity_test_web)


        initView()
    }


    fun initView() {
        binding!!.btn1.setOnClickListener(this)
        binding!!.btn2.setOnClickListener(this)
        binding!!.btn3.setOnClickListener(this)
        binding!!.btn4.setOnClickListener(this)
        binding!!.btn5.setOnClickListener(this)
        binding!!.btn6.setOnClickListener(this)
        binding!!.btn7.setOnClickListener(this)
        binding!!.btn8.setOnClickListener(this)



        (binding!!.actionBar as ActionBarView).updateAllContent(
            com.wink_172.library.R.mipmap.icon_back,
            "网络连接测试",
            ""
        );

        (binding!!.actionBar as ActionBarView).setCallback(object : ActionBarView.ICallback {
            override fun onSendEvent(event: Int, content: String?) {
                if (event == ActionBarView.LEFT_BTN) {
                    finish()
                }
            }


        })
    }

    override fun onClick(v: View) {
        super.onClick(v)

        when (v.id) {
            R.id.btn1 -> {
                val url = (v as TextView).text.toString()
                eventCheck(url, v,binding!!.hint1)
            }
            R.id.btn2 -> {
                val url = (v as TextView).text.toString()
                eventCheck(url, v,binding!!.hint2)
            }
            R.id.btn3 -> {
                val url = (v as TextView).text.toString()
                eventCheck(url, v,binding!!.hint3)
            }
            R.id.btn4 -> {
                val url = (v as TextView).text.toString()
                eventCheck(url, v,binding!!.hint4)
            }
            R.id.btn5 -> {
                val url = (v as TextView).text.toString()
                eventCheck(url, v,binding!!.hint5)
            }
            R.id.btn6-> {
                val url = (v as TextView).text.toString()
                eventCheck(url, v,binding!!.hint6)
            }
            R.id.btn7 -> {
                val url = (v as TextView).text.toString()
                eventCheck(url, v,binding!!.hint7)
            }
            R.id.btn8-> {
                val url = (v as TextView).text.toString()
                eventCheck(url, v,binding!!.hint8)
            }


            else -> {


            }
        }
    }

    fun eventCheck(url: String, v: View, hint: TextView) {
        HttpRequest.checkNetWorkEnable(url,
            object :
                com.wink_172.library.callback.ICallback {
                override fun onSendEvent(
                    event: Int,
                    vararg args: Any?
                ) {//这个callback用的非常巧妙    //这里因为不能传递非Serialiazable,对象 所以需要精简代码

                    val str = hint.text.toString()
                    when (event) {
                        1 -> {
                            v.setBackgroundColor(resources.getColor(R.color.logcat_level_info_color))
                            hint.setText(str + " 1")
                        }
                        2 -> {
                            v.setBackgroundColor(resources.getColor(R.color.logcat_level_error_color))
                            hint.setText(str + " 2")
                        }
                        3 -> {
//        v.setBackgroundColor(resources.getColor(R.color.logcat_level_verbose_color))
                            hint.setText(str + " 3")
                        }
                        4 -> {
                            hint.setText(str + " 4")
                        }
                    }
                }
            }

        )

    }


}